// pages/register/index.js
import {
  getMobileCode,
  resetPassword
} from '../../utils/api'
import {
  mobileValidate
} from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
      codeStatus: "获取验证码",
      time: 61,
      mobile: '',
      pwd: '',
      pwd_two: '',
      code: '',
      isShow: false,
      disabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
          invitecode: options.invitecode
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onMobileInput: function (event) {
      this.setData({
          mobile: event.detail.value
      })
  },
  onPwdInput: function (event) {
      this.setData({
          pwd: event.detail.value
      })
  },
  onPwdTwoInput: function (event) {
      this.setData({
          pwd_two: event.detail.value
      })
  },
  onCodeInput: function (event) {
      this.setData({
          code: event.detail.value
      })
  },
  bindSelect: function () {
      this.setData({
          isSelect: !this.data.isSelect
      })
  },

  bindCode: function () {
      const {
          mobile
      } = this.data
      if (!mobile) {
          wx.showToast({
              title: "请输入手机号码",
              icon: "none",
          })
      } else {
          if (mobileValidate(mobile)) {
              if (this.data.time == 61) {
                  this.postSms();
              }
          } else {
              wx.showToast({
                  title: "请输入正确的手机号码",
                  icon: "none",
              })
          }
      }
  },
  bindInviteCode(e) {
      this.setData({
          invitecode: e.detail.value
      })
  },
  bindRegister: function () {
      const {
          mobile,
          pwd,
          pwd_two,
          code,
      } = this.data
      if (mobile && mobileValidate(mobile) && pwd && pwd == pwd_two && code) {
          this.postRegister();
      } else {
          wx.showToast({
              title: !mobile ? '请输入手机号' : !mobileValidate(mobile) ? '手机号格式不正确' : !pwd ? '请输入密码' : pwd.length < 6 ? '密码不能小于6位数' : !pwd_two ? '请输入确认密码' : pwd != pwd_two ? '密码不一致' : !code ? '请输入验证码' :'',
              icon: 'none'
          })
      }
  },
  // POST 发送验证码
  postSms: function () {
      let that = this
      if (!that.data.disabled) {
          that.setData({
              disabled: true
          })
          getMobileCode(that.data.mobile, 3).then(() => {
              that.setData({
                  disabled: false
              })
              if (that.data.time == 61) {
                  that.setData({
                      time: that.data.time - 1,
                      intervalTime: setInterval(function clock() {
                          that.setData({
                              time: that.data.time - 1
                          })
                          if (that.data.time < 0) {
                              clearInterval(that.data.intervalTime);
                              that.setData({
                                  intervalTime: null,
                                  time: 61,
                                  codeStatus: '重新获取验证码'
                              })
                              return
                          }
                          that.setData({
                              codeStatus: "倒计时" + that.data.time + "秒"
                          })
                      }, 1000)
                  })
              }
          }).catch(() => {
              that.setData({
                  disabled: false
              })
          })
      }
  },

  postRegister: function () {
      const {
          mobile,
          pwd,
          code,
          disabled
      } = this.data
      if (!disabled) {
          this.setData({
              disabled: true
          })
          resetPassword({
              mobile,
              code,
              password: pwd
          }).then(() => {
              this.setData({
                  disabled: false
              })
              wx.showToast({
                  title: '修改成功',
                  icon: 'none'
                })
              setTimeout(() => {
                  wx.redirectTo({
                      url: '/pages/login/index',
                  })
              }, 1500)
          }).catch(() => {
              this.setData({
                  disabled: false
              })
          })
      }
  },
})